//配置文件
define("cinf",function(require,exports,module) {

    exports.conf = {
            curPage:1,
            actionUrl:"http://"+window.location.host + "/Thirdparty/Wxsub/getClassList",
            curClaType:"new",
            classTypeId:"",
            moreLoad:false,
            curTotalPage:1,
            wxs_id:$("#wixinIDHid").val(),
            user_id:$("#userIDHid").val()
        }
})
//加载列表数据
define("ajaxmodel",["cinf","framtime"],function(require,exports,module) {

    var cinf = require('cinf');
    var framtime = require('framtime');

    function regList(data){

        var str='';

        for(var i=0; i<data.length;i++){
            var class_label = data[i]['class_label'] || [];
            var titStr = "";
            for(var j=0;j<class_label.length;j++){

                var tabStr = class_label[j].title;

                if(tabStr.length > 10){
                    tabStr = tabStr.substring(0,10) + "..."
                }

                titStr+='<span class="tit">' + tabStr + '</span>'
            }
            //查看的URL地址
            var userToken = $("#userTokenIDHid").val();
            if(userToken){
                var dataUrl = data[i].url + "&user_token="+userToken+"&uid="+cinf.conf.user_id+"&wxs_id="+cinf.conf.wxs_id;
            } else {
                var dataUrl = data[i].url + "&uid="+cinf.conf.user_id+"&wxs_id="+cinf.conf.wxs_id;
            }

            str+='<section class="detail-content"><a href="'+dataUrl+'">'
                +'<dl>'
                +'<dt><img src=" '+data[i].cover+' " class="defaultImg"></dt>'
                +'<dd class="d-list">'+ data[i].course_name +'</dd>'
                +'<dd class="d-title">'
                + titStr
                +'</dd>'
                +'<dd><span class="num-t">' +data[i].appoint_num+ '人已预约</span></dd>'
                +'</dl>'
                +'<section class="see"><section class="see-lt">'
                +'<img src="' +data[i].tea_pic+ '" class="lt-img">'
                +'<span class="tit">' + data[i].tea_name + '</span>'
                +'</section>'
                +'<section class="see-lt see-rt">'
                +'<span class="class just">'
                +'<i class="timeId" data-end-time="'+ data[i].start_time +'"></i>'
                +'</span>'
                +'<span class="see-btn">查看</span>'
                +'</section>'
                +'</section>'
                +'</a></section>'
        }

        if(!cinf.conf.moreLoad){
            $(".list-detail").html(str);
        } else {
            $(".list-detail").append(str);
        }
    }

    exports.ajaxModel = function () {

        if(cinf.conf.classTypeId){

            var dataObj = {
                dataCode:cinf.conf.curClaType,
                dataId:cinf.conf.classTypeId,
                page_id:cinf.conf.curPage,
                wxs_id:cinf.conf.wxs_id,
                user_id:cinf.conf.user_id
            }
        } else {

            var dataObj = {
                dataCode:cinf.conf.curClaType,
                page_id:cinf.conf.curPage,
                wxs_id:cinf.conf.wxs_id,
                user_id:cinf.conf.user_id
            }
        }

        $.ajax({
            url:cinf.conf.actionUrl,
            type:'post',
            dataType:'json',
            data:dataObj,
            success:function(res){
                if(res.status=="0"){
                    $(".list-detail").html("");

                    $(".list-empty").show();
                }else if(res.status=="1"){

                    console.log("加载数据成功");
                    regList(res.data);
                    cinf.conf.curTotalPage = parseInt(res.total_no);

                    if(cinf.conf.curTotalPage > cinf.conf.curPage){
                        $("#pullUp").show();
                        cinf.conf.moreLoad = true;
                    }
                    $(".list-empty").hide();
                }
                framtime.countTime();
            }
        });
    }

})
//首页列表事件
define("indexclass",["cinf","ajaxmodel"],function(require,exports,module) {

    var cinf = require('cinf');
    var ajaxmodel = require('ajaxmodel');

    exports.listPageInit = function () {

        $(".list-nav .newList").click(function(){
            var index=$(this).index();
            if(index=="0" || index=="1"){
                $(this).addClass("on").siblings().removeClass("on");
                if($(this).hasClass("on")){
                    $(".more-list").hide();
                    $(".more-list li").removeClass("on");
                }
            }else if(index=="2"){
                $(this).addClass("on");
            }
        });
        $(".nav-icon").click(function(){
            var moreList=$(this).find(".more-list");
            $(".list-nav .newList").removeClass("on");
            moreList.toggle();

            if(moreList.css("display") == "block"){
                $(this).addClass("on");
            }else if(moreList.find("li").hasClass("on")){
                $(this).addClass("on");
            }else{
                $(this).removeClass("on");
            }
        });
        $(".more-list li").click(function(){
            $(this).addClass("on").siblings().removeClass("on");
            if($(this).hasClass("on")){
                $(".list-nav li").eq("0").removeClass("on");
                $(".list-nav li").eq("1").removeClass("on");
            }
        });

        //导航条标签点击
        $("#newData,#hostData").click(function(){

            var dataUrl=$("#list-nav").attr("data-url");
            var dataCode = $(this).attr('data-code');

            cinf.conf.actionUrl = "http://"+window.location.host + dataUrl;
            cinf.conf.curClaType = dataCode;
            cinf.conf.curPage = 1;
            cinf.conf.classTypeId = "";
            cinf.conf.moreLoad = false;

            $("#pullUp").hide();

            ajaxmodel.ajaxModel();

        });
        $("#listData li").click(function(){

            var dataUrl=$("#list-nav").attr("data-url");
            var dataCode = $(this).parents("#listData").attr('data-code');
            var dataId = $(this).attr('data-id');

            cinf.conf.actionUrl = "http://"+window.location.host + dataUrl;
            cinf.conf.curClaType = dataCode;
            cinf.conf.curPage = 1;
            cinf.conf.classTypeId = dataId;
            cinf.conf.moreLoad = false;
            $("#pullUp").hide();
            ajaxmodel.ajaxModel();

        });

        cinf.conf.curTotalPage = $("#totalPageHid").val();

        //显示加载更多导航条
        if(cinf.conf.curTotalPage > 1){
            cinf.conf.moreLoad = true;
            $("#pullUp").show();
        }

    }

})
//首页下拉刷新
define("indexscroll",["cinf","ajaxmodel"],function(require,exports,module) {

    var cinf = require('cinf');
    var ajaxmodel = require('ajaxmodel');

    var myScroll,
        pullDownOffset,
        pullUpEl, pullUpOffset;

    /**
     * 滚动翻页 （自定义实现此方法）
     * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
     */
    function pullUpAction () {

        setTimeout(function () {

            if(cinf.conf.curTotalPage > cinf.conf.curPage){
                cinf.conf.curPage = parseInt(cinf.conf.curPage) + 1;
                ajaxmodel.ajaxModel();
                myScroll.refresh();

                if(cinf.conf.curTotalPage == cinf.conf.curPage){
                    $("#pullUp").hide();

                }

            } else {
                cinf.conf.moreLoad = false;
                $("#pullUp").hide();
            }
            console.log("当前页数 %s",cinf.conf.curPage);

        }, 1000);
    }

    /**
     * 初始化iScroll控件
     */
    function loaded() {
        //pullDownEl = document.getElementById('pullDown');
        //pullDownOffset = pullDownEl.offsetHeight;
        pullUpEl = document.getElementById('pullUp');
        pullUpOffset = pullUpEl.offsetHeight;

        myScroll = new iScroll('wrapper', {
            scrollbarClass: 'myScrollbar', /* 重要样式 */
            useTransition: false,
            topOffset: pullDownOffset,
            onRefresh: function () {
               if (pullUpEl.className.match('loading')) {
                    pullUpEl.className = '';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                }
            },
            onScrollMove: function () {

                if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                    pullUpEl.className = 'flip';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
                    this.maxScrollY = this.maxScrollY;
                } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                    pullUpEl.className = '';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                    this.maxScrollY = pullUpOffset;
                }
            },
            onScrollEnd: function () {

                if (pullUpEl.className.match('flip')) {
                    pullUpEl.className = 'loading';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
                    pullUpAction();	// Execute custom function (ajax call?)
                }
            }
        });

        setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
    }

    exports.iscrollLoad = function () {

       if( $("#pullUp").length < 1) return;

        if(cinf.conf.curTotalPage == cinf.conf.curPage){
            $("#pullUp").hide();
            cinf.conf.moreLoad = false;
            return;
        }

        console.log("当前总页数 %s",cinf.conf.curTotalPage);

        document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

        loaded();

    }

})
/*网页多个倒计时*/
define("framtime",function(require,exports,module) {

    // function dataTypeNum(){
    //
    //     var dataTypeNum=$(".join-cls").find(".cls-rt").attr("data-type-num");
    //     if(dataTypeNum==0){//没有小助手
    //         $("#nowOrder").hide();
    //         $("#clsEnter").show();
    //         $("#clsEnter").click(function(){
    //             $(".classtips").show();
    //         });
    //     }else if(dataTypeNum==1){//有微信小助手
    //         $("#nowOrder").show();
    //         $("#clsEnter").hide();
    //         $("#nowOrder").click(function(){
    //             $(".successLayer").show();
    //         });
    //     }else if(dataTypeNum==2){//有QQ
    //         $("#nowOrder").show();
    //         $("#clsEnter").hide();
    //         $("#nowOrder").click(function(){
    //             $(".qqLayer").show();
    //         });
    //     }
    // }

    exports.countTime = function(){
        var Alarm = function (startime, endtime, countFunc, endFunc) {
            this.time = Math.floor((endtime - startime) / 1000); //时间
            this.countFunc = countFunc; //计时函数
            this.endFunc = endFunc; //结束函数
            this.flag = 't' + Date.parse(new Date()); //
        };
        Alarm.prototype.start = function () {
            var self = this;
            self.flag = setInterval(function () {
                if (self.time <=0) {
                    clearInterval(self.flag);
                    self.endFunc();
                    // console.log('计时结束');
                } else {
                    var minute, hour, day, second;
                    day = Math.floor(self.time / 60 / 60 / 24) < 10 ? '0' + Math.floor(self.time / 60 / 60 / 24) : Math.floor(self.time / 60 / 60 / 24);
                    hour = Math.floor(self.time / 60 / 60 % 24) < 10 ? '0' + Math.floor(self.time / 60 / 60 % 24) : Math.floor(self.time / 60 / 60 % 24);
                    minute = Math.floor(self.time / 60 % 60) < 10 ? '0' + Math.floor(self.time / 60 % 60) : Math.floor(self.time / 60 % 60);
                    second = Math.floor(self.time % 60) < 10 ? '0' + Math.floor(self.time % 60) : Math.floor(self.time % 60);
                    //倒计时执行函数
                    self.countFunc(second, minute, hour, day);
                    self.time--;
                }
            },1000);
        }

        /*服务器时间*/
        var serverTime=$(".join-cls .detaT,.cls-list").attr("data-server-time");
        var tYear=serverTime.substr(0,4);
        var tMonth=serverTime.substr(5,2);
        var tDate=serverTime.substr(8,2);
        var todayTime=tYear+'-'+tMonth+"-"+tDate;
        //调用
        var countTime = function () {
            var eles = $('.timeId'),  //eles是放置里面会有自定义data，end-time
                len = eles.length;
            for (; len > 0; len--) {
                var ele = eles.eq(len - 1);
                (function (ele) {
                    startTime = new Date(serverTime).getTime();
                    endTime = new Date(ele.attr('data-end-time')).getTime(),
                        alarm = new Alarm(startTime, endTime, function (second, minute, hour, day) { //计时钟
                            var dayInt=parseInt(day);
                            var dataStr=ele.attr('data-end-time');
                            var yearTime=dataStr.substr(0,4);
                            var monthTime=dataStr.substr(5,2);
                            var dataTime=dataStr.substr(8,2);
                            var dayTime=yearTime+'-'+monthTime+"-"+dataTime;
                            var differ=dataTime-tDate;
                            //添加类
                            var addClsLater=function(){
                                ele.parent().addClass("laterImg");
                            }
                            var addClsJust=function(){
                                ele.parent().removeClass("just");
                            }
                            //判断年月日是否属于当天
                            if(dayTime==todayTime){//年月日属于当天
                                if(dayInt == 0 && hour==00 && minute==00){//1分钟内
                                    addClsLater();
                                    ele.text(second+'秒后开课');
                                }else if(dayInt == 0 && hour==00){//1小时内
                                    addClsLater();
                                    ele.text(minute+'分钟后开课');
                                }else if(dayInt == 0 && hour>00){//1天内
                                    addClsJust();
                                    var dataEndTime=dataStr.substr(11,5);
                                    ele.text('今天'+dataEndTime);
                                }
                            }else{
                                //是否是当年或当月
                                if(yearTime==tYear && monthTime==tMonth){
                                    if(differ<2){
                                        addClsJust();
                                        var dataEndTime=dataStr.substr(11,5);
                                        ele.text('明天'+dataEndTime);
                                    }else if(differ>1){
                                        addClsJust();
                                        var dataEndTime=dataStr.substr(5,11);
                                        ele.text(dataEndTime);
                                    }
                                }else if(yearTime==tYear && monthTime!=tMonth){
                                    addClsJust();
                                    var dataEndTime=dataStr.substr(5,11);
                                    ele.text(dataEndTime);
                                }else if(yearTime!=tYear){
                                    addClsJust();
                                    var dataEndTime=dataStr.substr(0,11);
                                    ele.text(dataEndTime);
                                }
                            }
                        }, function () { //倒计时结束
                            ele.html("正在上课");
                            ele.parent().addClass("jstImg");
                        });
                    alarm.start();
                })(ele);
            }
        };
        countTime();

        var detaTime = function () {
            var eles = $('.detaT'), //eles是放置里面会有自定义data，end-time
                len = eles.length;
            for (; len > 0; len--) {
                var ele = eles.eq(len - 1);
                (function (ele) {
                    startTime = new Date(serverTime).getTime();
                    endTime = new Date(ele.attr('data-end-time')).getTime();
                    dataDiffer = new Date(ele.attr('data-differ-time')).getTime();
                    alarm = new Alarm(startTime, endTime, function (second, minute, hour, day) { //计时钟
                        var dayInt=parseInt(day);
                        var hourInt=parseInt(hour);
                        var dataStr=ele.attr('data-end-time');
                        var yearTime=dataStr.substr(0,4);
                        var monthTime=dataStr.substr(5,2);
                        var dataTime=dataStr.substr(8,2);
                        var dayTime=yearTime+'-'+monthTime+"-"+dataTime;
                        var differ=dataTime-tDate;
                        //添加类
                        var joinCls =function(){
                            ele.parent().addClass("nowCls");
                        }
                        //判断是否有小助手

                        //判断年月日是否属于当天
                        if(dayTime==todayTime){//年月日属于当天
                            //1小时内和1小时外
                            if(hourInt<1){//1小时内
                                if(dayInt == 0 && hourInt==0 && minute==00){//1分钟内
                                    ele.text(second+'秒后开课');
                                }else if(dayInt == 0 && hourInt==0){//1小时内
                                    ele.text(minute+'分钟后开课');
                                }
                                $("#clsEnter").show()
                                                .find(".entBtn").text("进入教室");
                                $("#clsEnter").click(function(){
                                    $(".enterLayer").show();
                                });
                                $("#nowOrder").hide();
                            }else{//1小时外
                                if(dayInt == 0 && hourInt>0){//1天内
                                    joinCls();
                                    var dataEndTime=dataStr.substr(11,5);
                                    ele.text('今天'+dataEndTime);
                                    $("#clsEnter").hide();
                                    $("#nowOrder").show();
                                }

                            }
                        }else{
                            //是否是当年或当月
                            if(yearTime==tYear && monthTime==tMonth){
                                //1天之外
                                if(differ<2){//明天
                                    joinCls();
                                    var dataEndTime=dataStr.substr(11,5);
                                    ele.text('明天'+dataEndTime);
                                }else if(differ>1){//超过明天
                                    joinCls();
                                    var dataEndTime=dataStr.substr(5,11);
                                    ele.text(dataEndTime);
                                }
                                $("#clsEnter").hide();
                                $("#nowOrder").show();
                            }else if(yearTime==tYear && monthTime!=tMonth){
                                joinCls();
                                var dataEndTime=dataStr.substr(5,11);
                                ele.text(dataEndTime);
                            }else if(yearTime!=tYear){
                                joinCls();
                                var dataEndTime=dataStr.substr(0,11);
                                ele.text(dataEndTime);
                                $("#clsEnter").hide();
                                $("#nowOrder").show();
                            }

                        }
                    }, function () { //倒计时结束
                        if(dataDiffer<startTime){
                            $(".dataDiffer").show();
                        }else{
                            $("#clsEnter").show();
                            $("#nowOrder").hide();
                            ele.html("正在上课");
                            ele.parent().addClass("justImg");
                            $("#clsEnter").click(function(){
                                $(".enterLayer").show();
                            });
                        }
                    });
                    alarm.start();
                })(ele);
            }
        };
        detaTime();
    }
})
/*详情页事件*/
define("detailclass",function(require,exports,module){

    function dataTypeNum(){

        var dataTypeNum=$("#wapReindIDHid").val();
        if(dataTypeNum==0){//没有小助手
            $(".nohelpLayer").show();
        }else if(dataTypeNum==1){//有微信小助手
            $(".successLayer").show();
        }else if(dataTypeNum==2){//有QQ
            $(".qqLayer").show();
        }
    }
    exports.detailPageInit = function () {

        $(".deta-img .share").click(function(){
            $(".shareLayer").show();
        });
        $("#shareWxBtn").click(function(){

            $(".shareLayer").show()
                              .find("i").removeClass("detaKeep");
            $("#nohelpLayer").hide();
        });
        $(".deta-img .collection").click(function(){
            $(".shareLayer").find(".detaShare").removeClass("detaKeep");
            $(".shareLayer").show();
        });
        $(".shareLayer").click(function(){
            $(this).hide();
        });
        $(".successLayer .delete").click(function(){
            $(".successLayer").hide();
        });
        $(".classtips .delete").click(function(){
            $(".classtips").hide();
        });
        $(".qqLayer .delete").click(function(){
            $(".qqLayer").hide();
        });
        $(".nohelpLayer .delete").click(function(){
            $(".nohelpLayer").hide();
        });

        $(".enterLayer .enterBtn").click(function(){
            var dataUrl=$(".enterLayer").attr("data-url-enter");
            var nick_name=$(".inputTest").val();
            var token=$(".token").val();
            $.ajax({
                url:dataUrl,
                type:'post',
                dataType:'json',
                data:{nick_name:nick_name,token:token},
                success:function(res){
                    if(res.code=="20001"){
                        $("#errTip").html("英文名长度只能是2-20位字符");
                    }else if(res.code=="20002"){
                        $("#errTip").html("英文名只能包含英文字母和空格");
                    }else if(res.code=="10000"){
                        $("#nickName").val(nick_name);
                        $("#toKen").val(token);
                        $("#formAction").submit();
                        $(".enterLayer").hide();
                    }
                }
            });
        });
        var dataStatus=$(".cls-details").attr("data-status");
        if(dataStatus=="1"){
            $(".join-bom").show();
            $(".app-bom").hide();
        }else if(dataStatus=="0"){
            $(".join-bom").hide();
            $(".app-bom").show();
        }

        //预约课程功能
        $("#orderBtn").click(function () {

            var dataUrl = "http://"+window.location.host+"/Thirdparty/Wxsub/add_appoint";

            $.ajax({
                url: dataUrl,
                type: 'post',
                dataType: 'json',
                data: {wxs_id: $("#wixinIDHid").val(), course_id: $("#courseIDHid").val()},
                success: function (res) {

                    if(res.code=="30000"){
                        $("#nowOrder").html("<a href='javascript:;' class='yiorderBtn' id='yiOrderBtn'>已预约</a>");
                        dataTypeNum();
                    } else {
                        alert("预约失败！");
                    }

                }
            })
        })
    }

})
//分享统计
define("shareweixin",function(require,exports,module) {
    /*微信分享*/
    var fixurl = window.location.host;
    var imgUrl = "http://www.51talk.com/static/acweb/release/images/shareIco.png";
    var lineLink = document.URL;
    var descContent = "想要全球Walk，请来51Talk！名师公开课就要开始啦，上课戳这里哦~";
    var shareTitle = $("#courseTitle").val();
    var appid = $("#appIdHid").val();

    //点击微信自带的分享的操作
    var shareweixin= function () {
        wx.config({
            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            appId: appid, // 必填，公众号的唯一标识
            timestamp: $("#timestampHid").val(), // 必填，生成签名的时间戳
            nonceStr: $("#nonceStrHid").val(), // 必填，生成签名的随机串
            signature: $("#signatureHid").val(),// 必填，签名，见附录1
            jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        });
        wx.ready(function () {
            //分享到朋友圈
            wx.onMenuShareTimeline({
                title: shareTitle, // 分享标题
                link: lineLink, // 分享链接
                imgUrl: imgUrl, // 分享图标
                success: function () {
                    // 用户确认分享后执行的回调函数
                    recordShare();
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
            //分享给好友
            wx.onMenuShareAppMessage({
                title: shareTitle, // 分享标题
                desc: descContent, // 分享描述
                link: lineLink, // 分享链接
                imgUrl: imgUrl, // 分享图标
                type: '', // 分享类型,music、video或link，不填默认为link
                dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                success: function () {
                    // 用户确认分享后执行的回调函数
                    recordShare();
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });

        })
    }

    // 在分享成功后，增加个回调函数
    function recordShare() {
        var courseId = $("#courseIDHid").val();
        var submitUrl = "http://www.51talk.com/Thirdparty/Wxsub/record_share";
        $.ajax({
            url: submitUrl,
            type: "POST",
            data: {
                course_id: courseId,
                page_type: "Class",
                channel: $("#channelIDHid").val()
            },
            success: function (info) {

                var result = info;

                if (result == "1") {

                    console.log("统计成功")
                } else {

                    return ""
                }

            }
        });
    }

    exports.shareWeixin = function () {
        shareweixin();
    }


})

$(document).ready(function () {

    seajs.use(["indexclass", "framtime", "detailclass", "shareweixin", "indexscroll"], function (indexclass, framtime, detailclass, shareweixin, indexscroll) {

        indexclass.listPageInit();
        framtime.countTime();
        detailclass.detailPageInit();
        shareweixin.shareWeixin();
        indexscroll.iscrollLoad();

    })
})